﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Smith.AMC.Configuration;

namespace MemcachedClientTests
{
    [TestFixture]
    public class ConfigurationTest
    {
        [Test]
        public void OpenDefaultConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            MemcachedClientConfiguration clientConfig = (MemcachedClientConfiguration)config.GetSection("smith.amc");
            Console.WriteLine(clientConfig.MemcachedNodes.Count);
        }

        [Test]
        public void DuplicateHostnamePortCombinationsShouldNotBeReturned()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            MemcachedClientConfiguration clientConfig = (MemcachedClientConfiguration)config.GetSection("smith.amc.duplicates");
            Assert.That(clientConfig.MemcachedNodes.Count, Is.EqualTo(1));
        }

        [Test]
        public void InvalidServersShouldCauseConfigNotToLoad()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            Assert.Throws<ConfigurationErrorsException>(() => config.GetSection("smith.amc.invalidservers"));

        }

        [Test]
        public void NotAddingValuesShouldUseDefaultValues()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            MemcachedClientConfiguration clientConfig = (MemcachedClientConfiguration)config.GetSection("smith.amc.defaults");

            Assert.That(clientConfig.UseUdp, Is.False);
            Assert.That(clientConfig.ReconnectPeriod, Is.EqualTo(10));
            Assert.That(clientConfig.ConnectionTimeout, Is.EqualTo(5));

            Assert.That(clientConfig.MemcachedNodes.Count, Is.EqualTo(1));
            Assert.That(clientConfig.MemcachedNodes[0].TcpPortNumber, Is.EqualTo(11211));
            Assert.That(clientConfig.MemcachedNodes[0].UdpPortNumber, Is.EqualTo(11212));
            Assert.That(clientConfig.NumberOfSockets, Is.EqualTo(25));
        }

        [Test]
        public void LoadingConfigFromExternalFileShouldWork()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            MemcachedClientConfiguration clientConfig = (MemcachedClientConfiguration)config.GetSection("smith.amc.file");
            Assert.That(clientConfig.MemcachedNodes.Count, Is.EqualTo(1));
        }

        [Test]
        public void MonitorConfigurationShouldWork()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            MemcachedClientConfiguration clientConfig = (MemcachedClientConfiguration)config.GetSection("smith.amc.monitor");
            Assert.That(clientConfig.MonitorConfiguration, Is.Not.Null);
            Assert.That(clientConfig.MonitorConfiguration.FailedThreshold, Is.EqualTo(989));
            Assert.That(clientConfig.MonitorConfiguration.CheckInterval, Is.EqualTo(new TimeSpan(0,1,10)));
        }

    }
}
